<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>
      powerBi报表一键导入生效功能
    </title>
    <script type="text/javascript" th:src="@{/scripts/jquery.min.js}"></script>
    <script type="text/javascript" th:src="@{/scripts/jquery.form.js}"></script>
    <style>
     .contain{width:100%;}
     .contain p{font-size:24px;font-weight:bold;text-align:center;overflow:hidden;}
     .g_zheng{width:50%;float:left;}
     .g_fen{width:50%;float:left;}
     .g_ge{width:60%;margin:0 auto;margin-bottom:20px;}
     .g_bu{height:100px;width:100%;overflow:hidden;margin-top:20px;}
     .g_bu textarea {resize:none;  width:250px; height:50px;margin-left:40px;border:1px groove #297FC3;}
     .g_fwen,.g_shu{font-size:14px;color:#333333;line-height:80px;padding-left:40px;}
     .g_wang{height:200px;}
     .g_shu{overflow:hidden;height:80px;width:100%;}
     .g_fzi{display:block;float:left;margin-top:-10px;}
     .g_fwen,.g_fzi{width:80px;}
     .g_ge{overflow:hidden;}
     .g_ge span{font-size:14px;color:#333333;width:143px;display:block;float:left;}
     .g_ge input{width:250px;height:20px;line-height:20px;}
     .g_cli{margin-bottom:10px;border:0;width:150px;border-radius:20px;background:#89A0C9;color:white;height:30px;line-height:30px;}
     .g_ge .g_cli{margin-bottom:10px;border:0;width:150px;border-radius:20px;background:#89A0C9;color:white;height:30px;line-height:30px;}
    </style>
    <script type="text/javascript">
      $(function(){
        $("#import").click(function(){
          postdata();
          });
          
        $("#actGetImportStatus").click(function(){
          postGetImport();
          });
          
        $("#actGetGateWayDs").click(function(){
          postGetGateWayDs();
          });
          
        $("#actPatchDatasource").click(function(){
          postPatchDatasource();
          });
          
        $("#executeAll").click(function(){
        	if(!validUser()) return false;
        	postdata();        	
        	setTimeout(function(){
		        // doing more async stuff
		        console.log('sleep 750!');
		        postGetImport();
		        if(!postGetGateWayDs()){
		        	return;
		        }
		        if(!postPatchDatasource()){
		        	return;
		        }
	    	}, 750);	    	        	       	       			        	      	       	 	          	
          });
          
          $("#actDeleteDataset").click(function(){
           postDeleteDataSet();
          });
          
          $("#clearInfo").click(function(){
          	clearInfo();
          });
       });
          
        //1.报表导入            
		function postdata(){	
           if(!notEmpty("displayName","请输入数据集名!")) return false;
           if(!notEmpty("accessKey","请输入访问键!")) return false;
           if($("#upload").val() == ''){
            alert("请设置pbix文件！"); 
            return  false; 
           }          
           if(!judgeIs()) return false;                     
		   var formData = new FormData($('#postForm')[0]);		  	       
	        $.ajax({
			         type:'POST',
			         url: '/powerbitool/report/pbix',
			         data: formData,
			         async:false,
			         //attention!!!
			         contentType: false,
			         cache: false,
			         processData:false,
			         success:function(data){
			           $("#importMsg").val(data);
			           if(isJson(data)){
				           var result=JSON.parse(data);
				           $("#importPbix").val(result.id);	
			           }else{
			           	   $("#importPbix").val('');	
			           }
			           return true;
			         },
			         error:function(data){
			           $("#importMsg").val(data);
			           return  false; 
			         }
	    	});    
		}
		
		//2.获取导入状态
		function postGetImport(){			
           if(!notEmpty("importPbix","请设置导入ID!")) return false;
           if(!notEmpty("accessKey","请输入访问键!")) return false;
          //ajax submit.
          var urlStr = "report/status?collectionName="+encodeURIComponent($("#collectionName").val())
                +"&workspaceID="+encodeURIComponent($("#workspaceID").val())+"&importId="+encodeURIComponent($.trim($("#importPbix").val()))+"&accessKey="+encodeURIComponent($.trim($("#accessKey").val()));  
          console.log(urlStr);
          return $.ajax({
                type:"get",
                url:urlStr,
                data:null,
                async:false,//是否异步
                success:function(data){
                	$("#datasetMsg").val(data);
                	if(isJson(data)){
	                	var result=JSON.parse(data);
		                $("#reportID").val(result.reports[0].id);
		                $("#datasetID").val(result.datasets[0].id);
                	}else{
                		$("#reportID").val('');
		                $("#datasetID").val('');
                	}
                return true;                                      
                },
                error:function(){
               		$("#datasetMsg").val(data);
                	return false;                   
                }
         });
		}
		
		//3.获取网关及数据源信息
		function postGetGateWayDs(){
           if(!notEmpty("datasetID","请设置数据集ID！")) return false;
           if(!notEmpty("accessKey","请输入访问键!")) return false;
          //ajax submit.
          var urlStr = "report/gatewayds?collectionName="+encodeURIComponent($("#collectionName").val())
                +"&workspaceID="+encodeURIComponent($("#workspaceID").val())+"&datasetID="+encodeURIComponent($.trim($("#datasetID").val()))+"&accessKey="+encodeURIComponent($.trim($("#accessKey").val()));                   
          console.log(urlStr);
          return $.ajax({
                type:"get",
                url:urlStr,
                data:null,
                async:false,//是否异步
                success:function(data){
                	$("#gatewayMsg").val(data);
                	if(isJson(data)){
	                	var result = JSON.parse(data);
	                	$("#gatewayID").val(result.value[0].gatewayId); 
	                	$("#datasourceID").val(result.value[0].id);
                	}else{
                		$("#gatewayID").val('');
		                $("#datasourceID").val('');
                	}
                	return true;                                                   
                },
                error:function(){
                	$("#gatewayMsg").val(data);
                	return false;                    
                }
         });
		}
		
		//4.更新数据源凭据
		function postPatchDatasource(){
           if(!notEmpty("gatewayID","请设置网关ID！")) return false;
           if(!notEmpty("datasourceID","请设置数据源ID！")) return false;
           if(!notEmpty("accessKey","请输入访问键!")) return false;
           if(!validUser()) return false;
           
           //ajax submit.
          var urlStr = "report/datasource?collectionName="+encodeURIComponent($("#collectionName").val())
                +"&workspaceID="+encodeURIComponent($("#workspaceID").val())+"&gatewayID="+encodeURIComponent($.trim($("#gatewayID").val()))
                +"&datasourceID="+encodeURIComponent($.trim($("#datasourceID").val()))+"&userName="+encodeURIComponent($.trim($("#userName").val()))
                +"&password="+encodeURIComponent($.trim($("#password").val()))+"&accessKey="+encodeURIComponent($.trim($("#accessKey").val()));                   
          console.log(urlStr);
          return $.ajax({
                type:"get",
                url:urlStr,
                data:null,
                async:false,//是否异步
                success:function(data){                	
                	$("#patchDatasource").val(data);
                	return true;
                },
                error:function(){
                	$("#patchDatasource").val(data);
                	return false;                    
                }
         });
		}
		
		function postDeleteDataSet(){
			if(!notEmpty("datasetID","请设置数据集ID！")) return false;
            if(!notEmpty("accessKey","请输入访问键!")) return false;
             //ajax submit.
          var urlStr = "delete/dataset?collectionName="+encodeURIComponent($("#collectionName").val())
                +"&workspaceID="+encodeURIComponent($("#workspaceID").val())+"&datasetID="+encodeURIComponent($.trim($("#datasetID").val()))+"&accessKey="+encodeURIComponent($.trim($("#accessKey").val()));                   
          console.log(urlStr);
          return $.ajax({
                type:"get",
                url:urlStr,
                data:null,
                async:false,//是否异步
                success:function(data){
                	$("#deleteDataSet").val(data);
                	if(data == '200'){
                		$("#datasetID").val('');
                		$("#datasetMsg").val('');
                		$("#reportID").val('');
                	}             	
                	return true;                                                   
                },
                error:function(){
                	$("#deleteDataSet").val(data);
                	return false;                    
                }
         });
		}
          //校验上传文件是否为pbix文件
          function judgeIs() {
            var strs = new Array(); //定义一数组
            var pic1= $("#upload").val();
            strs = pic1.split('.');
            var suffix = strs [strs.length - 1];

            if (suffix != 'pbix') {
	        	alert("你选择的不是pbix文件！");
	        	var file = document.getElementById("upload");
	        	file.outerHTML = file.outerHTML;
	        	return false;
            }else{
            	return true;
            }
          }

		  //检查elemName合法性，非法则提示info
          function notEmpty(elemName,info){
            var datasetDisplayName = document.getElementById(elemName);
            var Uhot,Urh;
            if(datasetDisplayName.value==""){
              alert(info);
              datasetDisplayName.focus();
              return false;
              };
              return true ;
            }
           
           //检查数据源用户名密码合法性
           function validUser(){	           
	           if(!notEmpty("userName","请设置 数据源的用户名！")) return false;
	           if(!notEmpty("password","请设置 数据源的密码！")) return false;	           
	           return true;
           }
           
           //判断数据是否为json
           function isJson(str){
	           if (typeof str == 'string') {
		        try {
		            JSON.parse(str);
		            return true;
		        } catch(e) {
		            console.log(e);		            
		        }
	    	  }
	    	  return false;
           }
           
           function clearInfo(){
	           $("#importMsg").val('');
	           $("#importPbix").val('');
	           $("#datasetMsg").val('');
	           $("#reportID").val('');
	           $("#datasetID").val('');
	           $("#gatewayMsg").val('');
	           $("#gatewayID").val('');
	           $("#datasourceID").val('');
	           $("#patchDatasource").val('');
	           $("#deleteDataSet").val('');		                
           }
          </script>
        </head>
        <body>
          <div class="contain">
              <p style="padding-bottom:20px;">powerBi报表一键导入生效功能</p>
              <form method="post" enctype="multipart/form-data" id="postForm" target="hidden_frame">
		            <div class="g_zheng">
		              <p style="color:red;font-size:12px;margin-left:-300px;">*以下为必填选项</p>
		              <div class="g_ge">
		                 <span>collectionName:</span>
		                 <input type="text" name="collectionName" id="collectionName" placeholder="请输入工作区集合名" value="platform"/>
		              </div>
		              <div class="g_ge">
		                 <span>workspaceID:</span>
		                 <input type="text" name="workspaceID" id="workspaceID" placeholder="请输入工作区ID" value="bfe4e237-81f5-4c28-8056-e2633c647a74"/>
		              </div>
		              <div class="g_ge">
			              <span>datasetDisplayName:</span>
			              <input type="text" name="displayName" id="displayName" placeholder="请输入数据集名" />
		              </div>
		              <div class="g_ge">
		              	<span>accessKey:</span>
		              	<input type="text" name="accessKey" id="accessKey" placeholder="请输入访问键" value="O3RSXw3zVcbC37qBY4O59tVXNFYv7AmpWOSGGPbAp4z2BW/Qq19UJU5N5IH6kjp+SR29uZPxX2F959UsSPBVvw=="/>
		              </div>
		              <div class="g_ge">
		                  <span>数据源的用户名:</span>
		                  <input type="text" name="userName" id="userName" placeholder="请输入数据源的用户名" value="adm"/><br>
		              </div>
		              <div class="g_ge">
		                  <span>数据源的密码:</span>
		                  <input type="text" name="password" id="password" placeholder="请输入数据源的密码" value="Hailiang888"/><br>
		              </div>
		              <div class="g_ge">
			              <span>.pbix 报表文件</span>
			              <input type="file" name="upload" id="upload">
		              </div>
		              <div class="g_ge">
		                 <input class="g_cli" id="executeAll" type="button" value="全部执行" size="30" />
		              </div>
		              <div class="g_ge" style="display:none">
				            <input class="g_cli" id="actDeleteDataset" type="button" value="删除报表" size="30" />   
				            <br> 
				            <textarea id="deleteDataSet" style="width:400px;"></textarea>
			          </div>
		          </div> 
		          <div class="g_fen"> 
		                <span style="font-size:18px;color:#333333;">分步执行</span>
		                <input class="g_cli" id="clearInfo" type="button" value="清除结果" size="30" />
			            <div class="g_bu">
				            <input class="g_cli" id="import" type="button" value="报表导入" size="30" /><br>             
				            <span class="g_fwen" style="display:block;float:left;">导入ID：</span>
				            <textarea id="importPbix" style="float:left;"></textarea>
				            <textarea id="importMsg" style="float:left;"></textarea>
				         </div>
				         <div class="g_bu g_wang">
				            <input class="g_cli" id="actGetImportStatus" type="button" value="获取报表ID" size="30" /><br>
				            <div class="g_shu">
					            <span class="g_fzi">数据集ID：</span>
					            <textarea id="datasetID"></textarea>
					            <textarea id="datasetMsg"></textarea>
				            </div>
				            <div class="g_shu">
					            <span class="g_fzi">报表ID：</span>				            				            
				            	<textarea id="reportID"></textarea>
				            </div>				            
				         </div>				         
			            <div class="g_bu g_wang">
				            <input class="g_cli" id="actGetGateWayDs" type="button" value="获取网关及数据源信息" size="30" /><br>           
				            <div class="g_shu">
					            <span class="g_fzi">网关ID：</span>
					            <textarea id="gatewayID"></textarea>
					            <textarea id="gatewayMsg"></textarea>
				            </div>
				            <div class="g_shu">
					            <span class="g_fzi">数据源ID：</span>
					            <textarea id="datasourceID"></textarea>
				            </div>
			            </div>
			            <div class="g_bu">
				            <input class="g_cli" id="actPatchDatasource" type="button" value="更新数据源凭据" size="30" />   
				            <br> 
				            <textarea id="patchDatasource" style="width:400px;"></textarea>
			            </div>
			            
		          </div>
		      </form>
          </div>
          
          <iframe name='hidden_frame' id="hidden_frame" style="display:none"></iframe>
        </body>
        
      </html>
